package com.example.springbootwebdemo.intercept;

import com.example.springbootwebdemo.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 */
public class LoginHandlerIntercept implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User user = (User) request.getSession().getAttribute("userInfo");
        if (user != null) {
            //登录状态放行
            return true;
        }
        request.setAttribute("login_error", "请先登录");
        request.getRequestDispatcher("/").forward(request, response);
        return false;
    }
}
